/*
 * @description: 
 * @Date: 2022-10-08 16:32:06
 * @LastEditTime: 2022-12-19 16:48:15
 */
import cache from "./cache";
import store from "./store";
import ComponentRouter from "./ComponentRouter";
//根据Id跳转
const redirectId = (id, open) => {
  const url = store.getters.find(id);//查找信息
  if (url) {
    redirectUrl(url.url)
  }
}
const opens = (url) => {
  let a = document.createElement("a");
  if (!(/http/.test(url))) {
    url = 'https://' + url
  }
  a.href = url;
  a.target = "_blank"
  a.click();
  a.remove();
}
const redirectUrl = (href, open = false) => {

  if (ComponentRouter[href]) {//匹配是否有内部组件路由，有则调用并启动内部组件
    ComponentRouter[href]();
    return;
  }

  if (open) {//是否新窗口打开，传递参数优先
    opens(href)
    return;
  }
  if (store.state.config.openType.linkOpen) {//新窗口打开，全局设置
    opens(href)
  } else {
    location.href = href;
  }
}
const searchGo = (text) => {
  if (ComponentRouter[text]) {//匹配是否有内部组件路由，有则调用并启动内部组件
    ComponentRouter[text]();
    return;
  }
  let s = cache.get("defSearch");
  let href = ""
  if (/^http/.test(text)) {
    href = text;
  } else {
    href = s.url + text
  }

  if (store.state.config.openType.searchOpen) {//新窗口打开
    opens(href)
  } else {
    location.href = href;
  }
}
export { redirectUrl, redirectId, searchGo }
export default redirectUrl;